package com.example.framework.manager;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.example.framework.utils.LogUtils;

import java.io.IOException;

/**
 * @author:0.2℃
 * @date： 2022-03-28 17:01
 * @Description ：媒体播放类
 */
public class MediaPlayerManager {
    private MediaPlayer mMediaPlayer;

    public static final int MEDIA_STATUS_PLAY = 0;  // 播放
    public static final int MEDIA_STATUS_PAUSE= 1;  // 暂停
    public static final int MEDIA_STATUS_STOP = 2;  // 停止

    public int MEDIA_STATUS = MEDIA_STATUS_STOP;   // 默认停止

    private onMusicProgressListener progressListener;
    private static final int HANDLER_PROGRESS = 1000;

    /**
     * 获取歌曲的进度回调
     * 计算歌曲的进度
     * 1. 开始播放的时候就开始循环计算时长
     * 2. 将进度计算结果对外抛出
     */
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case HANDLER_PROGRESS:
                    if (progressListener != null) {
                        // 拿到当前播放时长
                        int position = getCurrentPosition();
                        int pos = (int)(((float)position / (float)getDuration()) * 100);
                        progressListener.onProgress(position, pos);
                        // 再循环，每隔1s抛出一次
                        handler.sendEmptyMessageDelayed(HANDLER_PROGRESS, 1000);
                    }
            }
            return false;
        }
    });

    /**
     * 判断是否在播放的方法
     * @return
     */
    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    public MediaPlayerManager() {
        mMediaPlayer = new MediaPlayer();
    }

    /**
     * 播放方法
     *
     * @param path 使用AssetFileDescriptor这个类型的path，需要版本为安卓N以上
     */
    public void startPlayer(AssetFileDescriptor path) {
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(path.getFileDescriptor(), path.getStartOffset(), path.getLength());
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            MEDIA_STATUS = MEDIA_STATUS_PLAY;
            handler.sendEmptyMessage(HANDLER_PROGRESS);
        } catch (IOException e) {
            LogUtils.e(e.toString());
            e.printStackTrace();
        }
    }

    public void startPlayer(String path) {
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            MEDIA_STATUS = MEDIA_STATUS_PLAY;
            handler.sendEmptyMessage(HANDLER_PROGRESS);
        } catch (IOException e) {
            LogUtils.e(e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 暂停播放
     */
    public void pausePlayer() {
        if (isPlaying()) {
            mMediaPlayer.pause();
            MEDIA_STATUS = MEDIA_STATUS_PAUSE;
            handler.removeMessages(HANDLER_PROGRESS);
        }
    }

    /**
     * 继续播放
     */
    public void continuePlay() {
        mMediaPlayer.start();
        MEDIA_STATUS = MEDIA_STATUS_PLAY;
        handler.sendEmptyMessage(HANDLER_PROGRESS);
    }

    /**
     * 停止播放
     */
    public void stopPlayer() {
        mMediaPlayer.stop();
        MEDIA_STATUS = MEDIA_STATUS_STOP;
        handler.removeMessages(HANDLER_PROGRESS);
    }

    /**
     * 获取当前位置
     * @return
     */
    public int getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    /**
     * 获取音频总时长
     * @return
     */
    public int getDuration() {
        return mMediaPlayer.getDuration();
    }

    /**
     * 设置是否循环
     * @param isLooping
     */
    public void setLooping(boolean isLooping) {
        mMediaPlayer.setLooping(isLooping);
    }

    /**
     * 跳转到指定位置
     */
    public void seekTo(int seek) {
        mMediaPlayer.seekTo(seek);
    }

    /**
     * 播放完成
     * @param listener
     */
    public void setOnCompleteListener(MediaPlayer.OnCompletionListener listener) {
        mMediaPlayer.setOnCompletionListener(listener);
    }

    /**
     * 播放错误
     * @param listener
     */
    public void serOnErrorListener(MediaPlayer.OnErrorListener listener) {
        mMediaPlayer.setOnErrorListener(listener);
    }

    /**
     * 播放进度
     */
    public void setOnProgressListener(onMusicProgressListener listener) {
        progressListener = listener;
    }

    /**
     * 对外接口
     */
    public interface onMusicProgressListener {
        void onProgress(int position, int pos);
    }
}
